package com.itcat.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.OmsOrderApi;
import com.itcat.mapper.OmsOrderItemMapper;
import com.itcat.mapper.OmsOrderMapper;
import com.itcat.mapper.OmsOrderOperateHistoryMapper;
import com.itcat.mysql.param.DeliveryParam;
import com.itcat.mysql.param.OmsOrderParam;
import com.itcat.mysql.pojo.OmsOrder;
import com.itcat.mysql.pojo.OmsOrderItem;
import com.itcat.mysql.pojo.OmsOrderOperateHistory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class OmsOrderApiImpl /*extends ServiceImpl<OmsOrderMapper, OmsOrder>*/ implements OmsOrderApi {
    @Autowired
    public OmsOrderMapper omsOrderMapper;


    /**
     * 订单列表分页查询
     *
     * @param omsOrderParam
     * @return
     */
    public Page<OmsOrder> OrderList(OmsOrderParam omsOrderParam) {
        //创建分页对象
        Page<OmsOrder> pages = new Page<>(omsOrderParam.getPageNum(), omsOrderParam.getPageSize());
        //构建查询条件
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(omsOrderParam.getCreateTime() != null, OmsOrder::getCreateTime, omsOrderParam.getCreateTime());
        queryWrapper.like(omsOrderParam.getOrderSn() != null, OmsOrder::getOrderSn, omsOrderParam.getOrderSn());
        queryWrapper.eq(omsOrderParam.getOrderType() != null, OmsOrder::getOrderType, omsOrderParam.getOrderType());
        queryWrapper.eq(omsOrderParam.getReceiverKeyword() != null, OmsOrder::getBillReceiverPhone, omsOrderParam.getReceiverKeyword());
        queryWrapper.eq(omsOrderParam.getSourceType() != null, OmsOrder::getSourceType, omsOrderParam.getSourceType());
        queryWrapper.eq(omsOrderParam.getStatus() != null, OmsOrder::getStatus, omsOrderParam.getStatus());
        //执行分页查询
        Page<OmsOrder> omsOrderPage = omsOrderMapper.selectPage(pages, queryWrapper);
        return omsOrderPage;
    }

    @Autowired
    private OmsOrderItemMapper omsOrderItemMapper;
    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;


    /**
     * 批量删除订单信息
     *
     * @param ids
     */
    public void delete(Integer[] ids) {
        //将数组转化为集合
        List<Integer> list = Arrays.asList(ids);
        //调用omsOrderMapper执行批量删除
        omsOrderMapper.deleteBatchIds(list);
        //删除订单的同时，还要把和订单关联商品信息表和操作历史表删除
        LambdaQueryWrapper<OmsOrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(OmsOrderItem::getOrderId, ids);
        omsOrderItemMapper.delete(queryWrapper);

        LambdaQueryWrapper<OmsOrderOperateHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OmsOrderOperateHistory::getOrderId, ids);
        omsOrderOperateHistoryMapper.delete(wrapper);

    }


    /**
     * 批量关闭订单
     *
     * @param ids
     * @param
     */
    public void close(Integer[] ids, String note, Integer code) {
        //将数组转化为集合
        List<Integer> list = Arrays.asList(ids);
        //创建更新对象
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setNote(note);
        omsOrder.setStatus(code);
        //创建更新对象
        LambdaUpdateWrapper<OmsOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(list != null, OmsOrder::getId, list);
        updateWrapper.set(note != null, OmsOrder::getNote, note);
        updateWrapper.set(code != null, OmsOrder::getStatus, code);
        //执行查询
        omsOrderMapper.update(omsOrder, updateWrapper);
        //omsOrderMapper.updateById(omsOrder);
    }

    /**
     * 批量发货
     *
     * @param list
     * @param code
     */
    public void delivery(List<DeliveryParam> list, int code) {
        //获取list中的orderId
        List<Long> ids = CollUtil.getFieldValues(list, "orderId", Long.class);
        //遍历集合
        for (DeliveryParam deliveryParam : list) {
            // LambdaUpdateWrapper<OmsOrder> updateWrapper = new LambdaUpdateWrapper<>();
            OmsOrder omsOrder = new OmsOrder();
            omsOrder.setStatus(code);
            omsOrder.setDeliverySn(deliveryParam.getDeliverySn());
            omsOrder.setDeliveryCompany(deliveryParam.getDeliveryCompany());
            omsOrder.setId(Long.valueOf(String.valueOf(deliveryParam.getOrderId())));
          /*  updateWrapper.set(deliveryParam.getDeliveryCompany() != null, OmsOrder::getDeliveryCompany, deliveryParam.getDeliveryCompany());
            updateWrapper.eq(deliveryParam.getOrderId() != null, OmsOrder::getId, deliveryParam.getOrderId());
            updateWrapper.set(deliveryParam.getDeliverySn() != null, OmsOrder::getDeliverySn, deliveryParam.getDeliverySn());
            updateWrapper.set(OmsOrder::getStatus, code);*/
            omsOrderMapper.updateById(omsOrder);
        }

    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    public OmsOrder findOrder(Integer id) {
        //构建查询条件
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, OmsOrder::getId, id);
        OmsOrder omsOrder = omsOrderMapper.selectOne(queryWrapper);
        return omsOrder;
    }

    /**
     * 订单备注
     *
     * @param id
     * @param note
     * @param status
     */
    public void updateNote(Integer id, String note, Integer status) {
        LambdaUpdateWrapper<OmsOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(id != null, OmsOrder::getId, id);
        updateWrapper.set(note != null, OmsOrder::getNote, note);
        omsOrderMapper.update(null, updateWrapper);
    }

    /**
     * 修改收货人信息
     *
     * @param omsOrder
     */
    public void updateReceiverInfo(OmsOrder omsOrder) {
        omsOrderMapper.updateById(omsOrder);
    }
}
